<style>
.layui-form-pane .layui-form-label{width:auto;}
.layui-form-item{margin-bottom: 0px;}
.layui-table-cell{padding:1px 2px;text-align:center;font-size:13px;line-height:19px;}
.layui-table-view .layui-table td, .layui-table-view .layui-table th{padding:1px 0px;}
th span{white-space:normal;}
.finance tbody tr td {
    padding: 3px 0px !important;
    font-size: 12px !important;
}
.notprofit{color:red;}
</style>
<div class="fixed-bar">
	<form class="layui-form layui-form-pane fixed-bar-form" action="{:url()}" method="get" id="hisi-table-search">
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">订单号</label>
			<div class="layui-input-inline">
				<input type="text" name="client_ordernum" lay-verify="" placeholder="输入订单号" class="layui-input">
			</div>
		</div>
		{if env('env_site') == 'riwei' }
		<div class="layui-inline">
			<label class="layui-form-label">客户单号</label>
			<div class="layui-input-inline">
				<input type="text" name="ext_id" lay-verify="" placeholder="输入客户单号" class="layui-input">
			</div>
		</div>
		{/if}
		<div class="layui-inline">
			<label class="layui-form-label">SKU</label>
			<div class="layui-input-inline">
				<input type="text" name="sku" lay-verify="" placeholder="输入SKU" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">所属工厂</label>
			<div class="layui-input-inline">
				{:Form::select('factory_id', '', $factoryKV, '', '')}
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">订单状态</label>
			<div class="layui-input-inline">
				{:Form::select('order_status', '', ['' => '请选择', 1=>'未采购', 2=>'已采购', 3=>'已裁床', 4=>'已车缝', 5=>'已后整', 6=>'已出货', 7=>'已完成'], '', '')}
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">下单日期</label>
			<div class="layui-input-inline">
				<input type="text" name="create_date_range" readonly="" id="create_date_range" lay-verify="" placeholder="选择下单日期" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">完成日期</label>
			<div class="layui-input-inline">
				<input type="text" name="over_date_range" readonly="" id="over_date_range" lay-verify="" placeholder="选择完成日期" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">裁床日期</label>
			<div class="layui-input-inline">
				<input type="text" name="caichuang_over_date_range" readonly="" id="caichuang_over_date_range" lay-verify="" placeholder="选择裁床完成日期" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">搜索</button>
            <button type="button" class="layui-btn layui-btn-primary hisi-export">导出</button>
		</div>
	</div>
	</form>
</div>
<table id="dataTable" lay-filter='dataTable'></table>

{include file="system@block/layui" /}
<script type="text/html" id="createTimeTpl">
{{# if(d.create_time > 0){ }} 
	<span title="{{ layui.util.toDateString(d.create_time * 1000,'yyyy-MM-dd HH:mm')}}">{{ layui.util.toDateString(d.create_time * 1000,'MM-dd') }} </span>
{{# } }}
</script>
<script type="text/html" id="caichuangTimeTpl">
{{# if(d.create_time > 0){ }} 
	<span title="{{ layui.util.toDateString(d.caichuang_over_time * 1000,'yyyy-MM-dd HH:mm')}}">{{ layui.util.toDateString(d.caichuang_over_time * 1000,'MM-dd') }} </span>
{{# } }}
</script>
<script type="text/html" id="clientStatusTpl">
    {{# if(d.status == 99){ }}<span class="layui-badge">{{ d.delete_text }}</span><br> {{# } }}
	{{ clientStatus[d.client_status].replace(/生产中/, "") }}
</script>


<script type="text/javascript">
	var factoryKV = {:json_encode($factoryKV);};
	var groupKV = {:json_encode($groupKV);};
	var orderFrom = {:json_encode($orderFrom);};
	var clientStatus = {:json_encode($clientStatus);};
	groupKV['0'] = '请选择';
	
	layui.use([ 'opTable', 'jquery', 'tool', 'laydate', 'laytpl', 'form'], function() {
		var $ = layui.jquery;
		var laytpl = layui.laytpl;
		var form = layui.form;
		var laydate = layui.laydate;
		
		laydate.render({
		    elem: '#create_date_range'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
	    	,change: function(value, date, endDate){
		    	$('#create_date_range').val(value);
            	$('#layui-laydate1').remove();//关闭面板
            }
		});
		laydate.render({
		    elem: '#over_date_range'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
		    ,change: function(value, date, endDate){
		    	$('#over_date_range').val(value);
            	$('#layui-laydate2').remove();//关闭面板
            }
		});
		laydate.render({
		    elem: '#caichuang_over_date_range'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
		    ,change: function(value, date, endDate){
		    	$('#caichuang_over_date_range').val(value);
            	$('#layui-laydate3').remove();//关闭面板
            }
		});
		var opTable = layui.opTable.render({
			elem : '#dataTable'
			,url : '{:url()}'
			,page: true
			,limit: 50
            ,limits: [20,50,100,150,200]
            ,text: {
                none : '暂无相关数据'
            }
			,openVisible: false
			,toolbar: true
	        ,defaultToolbar: ['filter', 'exports', 'print']
			,cols : [[ 
				{field: 'defective_count', title:'裁剪日期',templet:function(d){ if(d.caichuang_over_time>0){ 
					return"<span title="+layui.util.toDateString(d.caichuang_over_time * 1000,'yyyy-MM-dd HH:mm')+">"+layui.util.toDateString(d.caichuang_over_time * 1000,'MM-dd')+"</span>";
				}else{
						return '--';
				}}}
				,{field: 'client_ordernum', title:'订单号',templet:function(d){ return'<span class="copy-item">'+d.client_ordernum+'</span>'}}
				,{field: 'user_account', title:'所属帐号',width:70,templet:function(d){ return orderFrom[d.is_auto_import];}}
				,{field: 'sku', title:'SKU/分配工厂',width:140,templet:function(d){ return'<span class="copy-item">'+d.sku+'</span>'+"<br>"+d.factory_name;}}
				,{field: 'mes_factory', title:'MES工厂',width:95}
				,{field: 'field0', title:'下单时间',templet:'#createTimeTpl'}
				,{field: 'field0', title:'订单数',templet: function(d){return  d.osStat.order_count;}}
				,{field: 'field0', title:'生产状态',templet:'#clientStatusTpl'}
				,{field: 'custom_material_cost', title:'<span title="订单物料的客户单价总和">MES面辅单价</span>'}
				,{field: 'material_cost_little', title:'MES物料费'}
				,{field: 'material_cost_total', title:'MES物料费(含利润)',width:80}
				,{field: 'cost', title:'MES加工费含倍率'}
				,{field: 'cost_profit', title:'MES加工费(含利润)',width:80}
				,{field: 'price', title:'<span title="MES平台-订单合算-总计(含总利润)">MES单件总计</span>'}
				,{field: 'material_cost_discuss_add', title:'<span title="">核价增加</span>'}
				,{field: 'field0', title:'<span title="采购总金额/实裁数">实际面辅料单价</span>',templet: function(d){
				var single_money=d.osStat.shicai_count>0 ? d.caigou_total_money/d.osStat.shicai_count : 0;return single_money.toFixed(2);}}
				,{field: 'custom_material_total', title:'<span title="(mes面辅单价+核价增加)*实裁数">MES面辅总金额</span>'}
				,{field: 'caigou_total_money', title:'<span title="订单各项采购物料总和">采购总金额</span>'}
				,{field: 'field0', title:'面辅赢亏情况',templet: function(d){
					var profit=d.custom_material_total-d.caigou_total_money;var labelClass=profit>=0?'isprofit':'notprofit';return "<span class='"+labelClass+"'>"+profit.toFixed(2)+"</span>";}
				}
				,{field: 'field0', title:'实裁数',templet: function(d){return  d.osStat.shicai_count;}}
				,{field: 'caichuang_user_name', title:'裁剪员'}
				,{field: 'special_process_name_str', title:'面辅二次工艺名称'}
				,{field: 'mes_process_price', title:'面辅二次工艺单价'}
				,{field: 'dress_cost_total', title:'成衣二次工艺合计'}
				,{field: 'dress_process_names', title:'成衣二次工艺名称'}
				,{field: 'dress_process_unitprices', title:'成衣二次工艺单价'}
				,{field: 'dress_process_counts', title:'成衣二次工艺用量'}
				,{field: 'accounting_diff_remark', title:'异常备注'}
				,{field: 'group_name', title:'组别'}
				,{field: 'field0', title:'车间数',templet: function(d){return  d.osStat.jiaohuo_count;}}
				//,{field: 'processing_charges', title:'MES加工费(含倍)'}
				,{field: 'sewing_charges', title:'<span title="车位价+打边价+冚车价">工厂针车位工价</span>'}
				,{field: 'MES_sweing_total', title:'MES金额'}
				,{field: 'factory_sweing_total', title:'下车位金额'}
				,{field: 'daohuo_count', title:'出货数',templet: function(d){return d.osStat.daohuo_count;}}  
				,{field: 'field0', title:'出货日期',templet:function(d){ if(d.tail_over_time>0){ 
					return"<span title="+layui.util.toDateString(d.tail_over_time * 1000,'yyyy-MM-dd HH:mm')+">"+layui.util.toDateString(d.tail_over_time * 1000,'MM-dd')+"</span>";
				}else{
						return '--';
				}}}
				,{field: 'field0', title:'MES入仓日期',templet:function(d){ if(d.store_time>0){ 
					return"<span title="+layui.util.toDateString(d.store_time * 1000,'yyyy-MM-dd HH:mm')+">"+layui.util.toDateString(d.store_time * 1000,'MM-dd')+"</span>";
				}else{
						return '--';
				}}}
				,{field: 'store_count', title:'MES入仓数',templet: function(d){return  d.store_count;}}
				,{field: 'fob_price', title:'MES FOB价',templet: function(d){return  '<span title='+d.sku_price+'>'+d.fob_price+'</span>';}}
				,{field: 'factory_shipment_amounts', title:'出货金额',templet: function(d){return  d.factory_shipment_amounts.toFixed(2);}}
				,{field: 'store_checkin_amounts', title:'入仓金额',templet: function(d){return  d.store_checkin_amounts.toFixed(2);}}
			]]
		});	
		// 库存进出
		var layerStockOutIn = null;
		$(document).on('click','.tail-stock-outin',function(){
			var orderId = $(this).attr('data-id');
			var url = $(this).attr('data-url');
			var json = $(this).attr('data-json');
			var d = [];
			d['list'] = eval("(" + json+ ")");
			d['url'] = url;
			d['order_id'] = orderId;
			var content = laytpl($('#stockOutInTpl').html()).render(d);
			layerStockOutIn = layer.open({
				type: 1
				,shade: 0
				,offset: '100px'
				,anim: parseInt(Math.random()*(6+1),10)
				,skin:'layui-layer-molv'
				,title:'库存进出'
				,content: content
			});
		});
		form.on('submit(stock-out-in)', function (data) {
			console.log(data.field);
			var url = data.field.url;
			$.ajax({
	 		   type: "POST",
	 		   url: url,
	 		   data: data.field,
	 		   dataType:"json",
	 		   success: function(res){
	 			  if (res.code == 1) {
	 				 $('#stockOutInTpl form')[0].reset();
	 				 layer.close(layerStockOutIn);
					 layer.msg(res.msg, {icon: 1,end:function(){opTable.reload();}});
				  } else {
					 layer.msg(res.msg, {icon: 2,anim: 6});
				  }
	 		   }
	 		});
			
			return false;
		});


		// 尾部完成
		var layerDone = null;
		$(document).on('click','.tail-done',function(){
			var orderId = $(this).attr('data-id');
			var url = $(this).attr('data-url');
			var json = $(this).attr('data-json');
			var d = [];
			d['list'] = eval("(" + json+ ")");
			d['url'] = url;
			d['order_id'] = orderId;
			var content = laytpl($('#doneTpl').html()).render(d);
			layerDone = layer.open({
				type: 1
				,shade: 0
				,offset: '100px'
				,anim: parseInt(Math.random()*(6+1),10)
				,skin:'layui-layer-molv'
				,title:'后整数录入'
				,content: content
			});
		});

	});
</script>